﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SelectDifficulty : MonoBehaviour {

    /// <summary>
    /// Called at the creation of the game.
    /// </summary>
    void Start()
    {
        // initialize options with the default values
        GameObject.Find("Easy").guiText.color = Color.green;
        PlayerPrefs.SetString("difficulty", "Easy");
    }

    /// <summary>
    /// Called when [mouse up].
    /// </summary>
    void OnMouseUp()
    {
        GameObject parent = gameObject.transform.parent.gameObject;
        if (parent.name == "Difficulty")
        {
            // get the transform property of all the children from parent
            Transform[] children = parent.GetComponentsInChildren<Transform>();
    
            // transform it in a list
            List<Transform> list = new List<Transform>(children);

            //remove the first element (the parent himself)
            list.RemoveAt(0);

            // get the current element selected and set it to unselected
            list.Find(l => l.gameObject.guiText.color == Color.green).gameObject.guiText.color = Color.white;

            // set the new element clicked to selected
            gameObject.guiText.color = Color.green;
            PlayerPrefs.SetString("difficulty", gameObject.name);
        }
    }

    /// <summary>
    /// Called when [mouse enter].
    /// </summary>
    void OnMouseEnter()
    {
        if (gameObject.guiText.color != Color.green)
        {
            gameObject.guiText.color = Color.cyan;
        }        
    }

    /// <summary>
    /// Called when [mouse exit].
    /// </summary>
    void OnMouseExit()
    {
        if (gameObject.guiText.color != Color.green)
        {
            gameObject.guiText.color = Color.white;
        }    
    }
}
